/********************************************
*
*
*
*  test_fidpid.c
*
*
********************************************/


#ifdef __cplusplus
 #if __cplusplus
  extern "C"{
 #endif
#endif /* __cplusplus */

#include "test_suite.h"
#include "d_fidpid.h"
#include "d_msg.h"

VOID DMS_SysPidMsgFunc(DMS_MSG_HEAD * pMsg)
{
    DMS_Show(0, "\r\npMsg = 0x%x \r\n", pMsg );
}


UINT32 TEST_DMS_SendMsg_00()
{
    DMS_MSG_HEAD * pMsg;
    UINT32  ulRet;

    pMsg = DMS_MsgAlloc(sizeof(DMS_MSG_HEAD));
    if(NULL == pMsg)
    {
        return DMS_ERR;
    }

    pMsg->ulRecvPID    = DMS_PID_SYS;
    pMsg->ulRecvProcID = DMS_LOCAL_CPUID;

    ulRet = DMS_MsgSend( DMS_PID_SYS , pMsg);
    CU_EQUAL(DMS_OK, ulRet);

    DMS_TaskDelay(1000);
    return DMS_OK;
}

typedef struct tagUserMsg{
    DMS_MSG_HEAD stMsgHead;
    UINT32 ulFlag;
    UINT32 ulNum;
}TEST_USER_MSG;

enum tagMsgType{
    TEST_MSG_COMM = 0x12345,
    TEST_MSG_END
};

VOID TEST_PidMsgFunc(DMS_MSG_HEAD * pMsg)
{
    static UINT32 ulCount = 0;
    static UINT32 ulLastMsgNum = 0;

    TEST_USER_MSG * pUserMsg = (TEST_USER_MSG*)pMsg;

    if( TEST_MSG_COMM == pUserMsg->ulFlag)
    {
        ulCount++;
    }
    else if (TEST_MSG_END == pUserMsg->ulFlag )
    {
        DMS_Show(0, "\r\n PID = %u , ulCount = %u \r\n", pMsg->ulRecvPID ,ulCount );
    }

    if(pUserMsg->ulNum >= ulLastMsgNum)
    {
        CU_EQUAL(0,0);
    }
    else
    {
        CU_EQUAL(0,1);
    }
}

VOID TEST_SendMsgTaskEntry(UINT32 ulParam,VOID * pParam)
{
    UINT32 ulCount;
    TEST_USER_MSG * pMsg;
    UINT32 ulRet;

    if(ulParam >= DMS_PID_BUT )
    {
        return;
    }

    for(ulCount = 0 ; ulCount < 10000 ; ulCount++)
    {
        while(NULL == (pMsg = (TEST_USER_MSG *)DMS_MsgAlloc(sizeof(TEST_USER_MSG))))
        {
            DMS_TaskDelay(10);
        }

        pMsg->stMsgHead.ulRecvPID    = ulParam;
        pMsg->stMsgHead.ulRecvProcID = DMS_LOCAL_CPUID;

        if(10000 - 1 == ulCount)
        {
            pMsg->ulFlag  = TEST_MSG_END;
        }
        else
        {
            pMsg->ulFlag  = TEST_MSG_COMM;
        }

        pMsg->ulNum  = ulCount;

        while(DMS_ERR_QUEUE_FULL == (ulRet = DMS_MsgSend( DMS_PID_SYS , (DMS_MSG_HEAD *)pMsg) ))
        {
            DMS_TaskDelay(10);
        }

        CU_EQUAL(ulRet , DMS_OK);
    }
}

UINT32 TEST_DMS_SendMsg_01()
{
    UINT32 ulRet;
    UINT32 i;
    char acTaskName[DMS_MAX_NAME_LEN];
    TASK_HANDLE TaskID[200];
    UINT32 ulTaskCount = 0;
    UINT32 ulParam;

    ulRet = DMS_PidInfoModify(DMS_PID_SYS,TEST_PidMsgFunc);
    CU_EQUAL(ulRet , DMS_OK);

    ulRet = DMS_PidInfoModify(DMS_PID_S1,TEST_PidMsgFunc);
    CU_EQUAL(ulRet , DMS_OK);

    ulRet = DMS_PidInfoModify(DMS_PID_S2,TEST_PidMsgFunc);
    CU_EQUAL(ulRet , DMS_OK);

    ulRet = DMS_PidInfoModify(DMS_PID_T1,TEST_PidMsgFunc);
    CU_EQUAL(ulRet , DMS_OK);

    ulRet = DMS_PidInfoModify(DMS_PID_T2,TEST_PidMsgFunc);
    CU_EQUAL(ulRet , DMS_OK);

    ulRet = DMS_PidInfoModify(DMS_PID_A1,TEST_PidMsgFunc);
    CU_EQUAL(ulRet , DMS_OK);

    ulRet = DMS_PidInfoModify(DMS_PID_A2,TEST_PidMsgFunc);
    CU_EQUAL(ulRet , DMS_OK);

    ulRet = DMS_PidInfoModify(DMS_PID_A3,TEST_PidMsgFunc);
    CU_EQUAL(ulRet , DMS_OK);

    ulRet = DMS_PidInfoModify(DMS_PID_A4,TEST_PidMsgFunc);
    CU_EQUAL(ulRet , DMS_OK);

    ulRet = DMS_PidInfoModify(DMS_PID_A5,TEST_PidMsgFunc);
    CU_EQUAL(ulRet , DMS_OK);

    ulParam = DMS_PID_SYS;

    for( i = 0 ; i < 10 ; i++ )
    {
        for( ulParam = DMS_PID_SYS ; ulParam < DMS_PID_BUT ; ulParam++ )
        {
            sprintf(acTaskName,"SendMsg%u",ulTaskCount);
            ulRet = DMS_TaskCreate(&TaskID[ulTaskCount],
                                   acTaskName,
                                   TEST_SendMsgTaskEntry,
                                   ulParam,
                                   NULL);
            CU_EQUAL(ulRet , DMS_OK);
            ulTaskCount++;

            if(ulTaskCount > 200)
            {
                i = 10;
                break;
            }
        }
    }

    for( i = 0 ; i< ulTaskCount ; i++ )
    {
        while(DMS_OK == DMS_TaskHandleValid(TaskID[i]))
        {
            DMS_TaskDelay(10);
        }
    }

    DMS_TaskDelay(1000);

    return DMS_OK;
}

UINT32 TEST_DMS_SendMsg_02()
{

    return DMS_OK;
}

UINT32 TEST_DMS_SendMsg_03()
{

    return DMS_OK;
}

VOID TEST_DMS_SendMsg()
{
    CU_ASSERT_EQUAL(TEST_DMS_SendMsg_00(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_SendMsg_01(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_SendMsg_02(),DMS_OK);
    CU_ASSERT_EQUAL(TEST_DMS_SendMsg_03(),DMS_OK);
}

VOID TEST_suite_fidpid(VOID)
{
    CU_pSuite suite;
    suite = CU_add_suite(__FUNCTION__, NULL, NULL);
    CU_ADD_TEST(suite, TEST_DMS_SendMsg);
}

#ifdef __cplusplus
 #if __cplusplus
  }
 #endif
#endif /* __cplusplus */
